getStudentList();
$(".startSearch").click(function() {
	getStudentList();
})
//获取所有学员
function getStudentList() {
	var machineCode = $(".machineCode").val();
	var gender = $(".gender option:selected").val();
	var studentName = $(".studentName").val();
	var teacherName = $(".teacherName").val();
	var className = $(".className option:selected").val();
	var blood = $(".blood option:selected").val();
	var operationType = $(".operationType option:selected").val();
	//	var number = $(".number").val();
	var number = 'BR16A';
	var constellation = $(".constellation").val();
	var startTime = $(".startTime").val();
	var endTime = $(".endTime").val();
	var sortOrder = $(".sortOrder option:selected").val();
	var grade=$('.grade').val();
	var type = $(".type option:selected").val();
//	var fieldSort = $(".fieldSort option:selected").val();
// var achievement=$('.achievement').val()
	layui.use(['table', 'laydate', 'laypage', 'layer'], function() {
		var laypage = layui.laypage,
			layer = layui.layer;
		var table = layui.table;
		var laydate = layui.laydate;
		//日期日期选择器
		laydate.render({
			elem: '.createTime',
			type: 'datetime'
		});
		laydate.render({
			elem: '.updateTime',
			type: 'datetime'
		});

		$('div[title="导出"]').click(function() {
			$(this).attr('lay-event', 'ads')
			alert(1)
		})
		var stamp = new Date().getTime();
		var systoken = sessionStorage.getItem('systoken');
		
		var param = {
			//machineCode: machineCode,
			gender: gender,
			name: studentName,
			// teacherName: teacherName,
			// className: className,
			// blood: blood,
			// operationType: operationType,
			// number: number,
			// constellation: constellation,
			type:type,
            grade:grade,
			startDate: startTime,
			endDate: endTime,
			sortOrder: sortOrder,
//			fieldSort: fieldSort,
			stamp: stamp,
			version: 0,
			visit_plat: 2,
			visit_type: 0,
			is_used: 0
		};
		param = JSON.stringify(param);
		var url = path + "/grade/getGrade";
		var defaultpageSise = 10;
		window.ins1 = _render(defaultpageSise);

		function _render(pageSize) {
			var width  =$(document).width();
			table.render({
				elem: '#stuTest',
				height: 600,
				title: '用户数据表',
				method: 'post',
				url: url,
				page: true,
				where: {param: param},
				headers: {Authorization: systoken},
				skin: 'row',
				even: true,
				autoSort: false,
				loading: true,
				parseData: function(res) {
					var data = res.rows
					for(i = 0; i < data.length; i++) {
						var gender = data[i].gender;

						if(gender = '0') {
							data[i].gender = '男';
						} else if(gender='1') {
							data[i].gender = '女';
						}
					}
                    var msg = '';
                    if (res.status == '000') {
                        msg = res.info;
                    } else if (res.rows=='后台数据为空') {
                        msg = '数据为空';
                    }
					return {
						"code": res.status,
						"msg": msg,
						"count": res.total,
						"data": res.rows
					};
				},
				done: function(res) {

					$('#inp').val(pageSize);
					$("#inp").on('blur', function() {
						var inputVal = $("#inp").val();
						_render(inputVal);
					});
				},
				totalRow: true,
				limit: 30,
				width:width*0.9,
//				toolbar: '.toolbarDemo',
				cols: [
					[
//					{
//							type: 'checkbox',
//							fixed: 'left',
//						},
						{
							field: 'name',
							title: '姓名',
							width:'33%'
						},
						// {
						// 	field: 'gender',
						// 	title: '性别',
                        //
						// },
						// {
						// 	field: 'blood',
						// 	title: '血型',
                        //
						// },
                        //
						// {
						// 	field: 'star',
						// 	title: '星座',
                        //
						// },

						{
							field: 'grade',
							title: '成绩',
							width:'33%'
						},{
							field: 'examDate',
							title: '考试日期',
							width:'33%'
						}
						
						// 					{
						// 						field: 'qrcode',
						// 						title: '二维码',
						// //						toolbar: '#barDemo1'
						// 						//图片不能太大再tr里展示的时候，否则复选框会对不起齐
						// 						templet:'<div><img src="{{ d.qrcode}}"></div>',style:'height:28px;width:28px!important;',
						// 						width: 150,
						// 					},
						// {
						// 	fixed: 'right',
						// 	title: '操作',
						// 	toolbar: '#barDemo',
                        //
						// }
					]
				],
				page: true ,
				limit: pageSize,
				loading: true,
			});
		};

		//工具栏事件
		table.on('toolbar(stuTest)', function(obj) {
			var checkStatus = table.checkStatus(obj.config.id);
			var exportExel = table.checkStatus('stuTest').data;
			switch(obj.event) {
				case 'add':
					layer.msg('添加');
					break;
				case 'update':
					layer.msg('编辑');
					break;
				case 'delete':
					layer.msg('删除');
					break;
				case 'getCheckData':
								
					var deletData = checkStatus.data;
					exportExel = checkStatus.data;
					batchPass(deletData);
					break;
				case 'getCheckLength':
					var data = checkStatus.data;
					layer.msg('选中了：' + data.length + ' 个');
					break;
				case 'isAll':
					layer.msg(checkStatus.isAll ? '全选' : '未全选')
					break;
				case 'LAYTABLE_EXPORT':
					table.exportFile('stuTest', exportExel, 'xls');
					break;
			};
		});
		//封装查看批量学员函数开始1203修改batchPass
		function batchPass(deletData, index) {
			var paramCheck = {
				deletData: deletData
			};
			var studentIds = [];
			var deletId = paramCheck.deletData;
			if(!deletId.length) {
				alert("请先选择需要查看的学员！");
				return;
			}
			for(var i = 0; i < deletId.length; i++) {
				studentIds.push(deletId[i].studentId);
				sessionStorage.setItem('studentIds',studentIds);
			}
			jumpExerciseNumber('学员分数', 'scoreDetailEcharts.html');
			getStudentList();
		}

		//监听行工具事件
		table.on('tool(stuTest)', function(obj) {

			var data = obj.data;

			//						var id = data.id;
			if(obj.event === 'del') {
				layer.confirm('确定删除这一行数据?', function(index) {
					//							这里编写删除的逻辑,下面两个步骤是完成与后台的交互后才执行的
					var id = data.id;
					obj.del();
					layer.close(index);
					//								删除表格
					deleteStudent();

					function deleteStudent() {
						var systoken = sessionStorage.getItem('systoken');
						var stamp = new Date().getTime();
						var version = 0;
						var visit_plat = 0;
						var visit_type = 0;
						var is_used = 0;
						var param = {
							id: id,
							stamp: stamp,
							version: version,
							visit_plat: visit_plat,
							visit_type: visit_type,
							is_used: is_used
						}
						param = JSON.stringify(param);
						var url = path + "/student/deleteStudent"
						$.ajax({
							url: url,
							type: "post",
							data: {
								param: param
							},
							dataType: "json",
							success: function() {
								getStudentList();
							}
						})
					}
				});
			} else if(obj.event === 'edit') {
				iframeData = data;
				editUser(iframeData);
			} else if(obj.event === 'detail') {
				layer.open({
					type: 2,
					title: '二维码',
					closeBtn: 0,
					shade: [0.001, '#393D49'],
					area: '100px',
					skin: 'layui-layer-nobg', //沒有背景色
					shadeClose: true,
					content: data.qrcode,
					id: id,
				})

			} else if(obj.event === 'studentPassRate') {
				var studentIds = data.id;
				var studentName=data.studentName
				sessionStorage.setItem('studentIds', studentIds);
				sessionStorage.setItem('studentName',studentName)
				jumpExerciseNumber('学员通过率', 'scoreDetailEcharts.html?id='+studentIds);
			}
			//监听排序
			table.on('sort(stuTest)', function(obj) {
				layer.msg('已经完成排序！ ' + obj.field + ' ' + obj.type);
				//服务端排序
				table.reload('stuTest', {
					initSort: obj
						//,page: {curr: 1}
						,
					where: {
						key: obj.field
							,
						order: obj.type
					}
				});
			});

			var $ = layui.jquery,
				active = {
					parseTable: function() {
						table.init('parse-table-demo', {
							limit: 3
						});
					},
					add: function() {
						table.addRow('stuTest')
					}
				};
			//table按钮（删除等）
			$('i').on('click', function() {
				var type = $(this).data('type');
				active[type] ? active[type].call(this) : '';
			});
			$('.layui-btn').on('click', function() {
				var type = $(this).data('type');
				active[type] ? active[type].call(this) : '';
			})
		})
	})
}

var iframeIndex;

//获取练习编号
getStudentCode();
function getStudentCode() {
	var stamp = new Date().getTime(); //数据时间戳
	var version = 0; //数据版本 (int),
	var visit_plat = 0; //访问平台 (int),
	var visit_type = 0; //访问类型 (int),
	var is_used = 0; // 数据是否有效 0有效1失效 (int)
	var systoken = sessionStorage.getItem('systoken');
	var studentIds = [];

	var param = {
		studentIds: studentIds,
		stamp: stamp,
		version: version,
		visit_plat: visit_plat,
		visit_type: visit_type,
		is_used: is_used
	}
	param = JSON.stringify(param);
	var url = path + '/PassingRate/getStudentCode';
	$.ajax({
		url: url,
		type: "post",
		data: {param: param},
		headers: {Authorization: systoken},
		dataType: "json",
		success: function(res) {
			//					获得所有练习编号
			var codes = res.rows;
			var length = codes.length;
			for(var i = 0; i < length; i++) {
				$(".number").append($("<option value=codes[i]>" + codes[i] + "<option>"))
			}
		}
	})
}
sessionStorage.removeItem("menu");
sessionStorage.removeItem("curMenu");